import { BUTTON_STATUS_NUMBER } from '@/config/button'
import { urlPrefix as userUrlPrefix } from '@/views/system/user/api'
import { request } from '@/api/service'

export const crudOptions = (vm) => {
  return {
    pageOptions: {
      compact: true
    },
    options: {
      tableType: 'vxe-table',
      rowKey: true, // 必须设置，true or false
      rowId: 'id',
      height: '100%', // 表格高度100%, 使用toolbar必须设置
      highlightCurrentRow: false

    },
    rowHandle: {
      view: {
        thin: true,
        text: '',
        disabled () {
          return !vm.hasPermissions('Retrieve')
        }
      },
      width: 250,
      edit: {
        thin: true,
        text: '',
        disabled () {
          return !vm.hasPermissions('Update')
        }
      },
      remove: {
        thin: true,
        text: '',
        disabled () {
          return !vm.hasPermissions('Delete')
        }
      },
      custom: [{
        text: ' 重置key',
        type: 'warning',
        size: 'small',
        emit: 'updateKey',
        disabled () {
          return !vm.hasPermissions('Update')
        }
      }]
    },
    indexRow: { // 或者直接传true,不显示title，不居中
      title: '序号',
      align: 'center',
      width: 100
    },

    viewOptions: {
      componentType: 'form'
    },
    formOptions: {
      defaultSpan: 24, // 默认的表单 span
      width: '35%'
    },
    columns: [{
      title: '关键词',
      key: 'search',
      show: false,
      disabled: true,
      search: {
        disabled: false
      },
      form: {
        disabled: true,
        component: {
          props: {
            clearable: true
          },
          placeholder: '请输入关键词'
        }
      },
      view: { // 查看对话框组件的单独配置
        disabled: true
      }
    },
    {
      title: 'ID',
      key: 'id',
      show: false,
      width: 90,
      form: {
        disabled: true
      }
    }, {
      title: '名称',
      key: 'name',
      width: 120,
      sortable: true,
      search: {
        disabled: false,
        component: {
          props: {
            clearable: true
          }
        }
      },

      type: 'input',
      form: {
        rules: [ // 表单校验规则
          { required: true, message: '名称必填项' }
        ],
        component: {
          placeholder: '请输入名称',
          props: {
            clearable: true
          }
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    }, {
      title: '关联用户',
      key: 'user',
      type: 'select',
      width: 100,
      dict: {
        url: userUrlPrefix,
        label: 'name',
        value: 'id',
        getData: (url, dict) => {
          return request({ url: url }).then(ret => {
            return ret.data
          })
        }
      },
      search: {
        disabled: false
      },
      form: {
        rules: [ // 表单校验规则
          { required: true, message: '关联用户必填项', trigger: 'blur' }
        ],
        component: {
          span: 24,
          props: {
            elProps: { // dict-select内部封装了el-select
              filterable: true // 可过滤选择项
            }
          },
          placeholder: '请选择关联用户'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    }, {
      title: 'access_key',
      key: 'access_key',
      sortable: true,
      search: {
        disabled: true
      },

      type: 'input',
      form: {
        disabled: true
      }
    }, {
      title: 'secret_key',
      key: 'access_secret',
      sortable: true,
      search: {
        disabled: true
      },
      type: 'input',
      form: {
        disabled: true
      }
    }, {
      title: '状态',
      key: 'status',
      sortable: true,
      width: 80,
      type: 'radio',
      dict: {
        data: BUTTON_STATUS_NUMBER
      },
      search: {
        disabled: false
      },
      form: {
        value: 1,
        rules: [
          { required: true, message: '是否状态不能为空', trigger: 'blur' }
        ],
        component: {
          placeholder: '请选择状态'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    }, {
      title: '备注',
      key: 'description',
      show: false,
      search: {
        disabled: true
      },
      type: 'textarea',
      form: {
        component: {
          placeholder: '请输入内容',
          showWordLimit: true,
          maxlength: '200',
          props: {
            type: 'textarea'
          }
        }
      }
    },
    {
      title: '创建人',
      show: false,
      width: 100,
      key: 'modifier_name',
      form: {
        disabled: true
      }
    },
    {
      title: '创建时间',
      key: 'create_datetime',
      type: 'datetime',
      width: 160,
      sortable: true,
      form: {
        disabled: true
      }
    }
    ]
  }
}
